﻿#region Copyright (c) 2008 plexBB Team

/*  Copyright (c) 2008 plexBB Team
 *  
 *  This file is free software: you may copy, redistribute and/or modify it  
 *  under the terms of the GNU General Public License as published by the  
 *  Free Software Foundation, either version 2 of the License, or (at your  
 *  option) any later version.  
 *  
 *  This file is distributed in the hope that it will be useful, but  
 *  WITHOUT ANY WARRANTY; without even the implied warranty of  
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
 *  General Public License for more details.  
 *  
 *  You should have received a copy of the GNU General Public License  
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

#endregion

using System;
using System.IO;
using System.Reflection;
using System.Web.UI;

namespace PlexBB
{
    internal static class Util
    {
        public static Stream GetResourceStream(string resourceLocalPath)
        {
            // secure resource local path
            resourceLocalPath = resourceLocalPath.Replace('\\', '/').TrimStart('/');
#if PLEXBB_PRECOMPILED
            string resourceName = "PlexBB." + resourceLocalPath.Replace('/', '.');
            Assembly assembly = Assembly.GetExecutingAssembly();
            return assembly.GetManifestResourceStream(resourceName);
#else
            string physicalPath =  HttpContext.Current.Server.MapPath("~/App_Code/PlexBB/" + resourceLocalPath);
            return File.Open(physicalPath, FileMode.Open, FileAccess.Read, FileShare.Read);
#endif
        }

        public static bool CompareByteArrays(byte[] data1, byte[] data2)
        {
            // If both are null, they're equal
            if (data1 == null && data2 == null)
            {
                return true;
            }
            // If either but not both are null, they're not equal
            if (data1 == null || data2 == null)
            {
                return false;
            }
            if (data1.Length != data2.Length)
            {
                return false;
            }
            for (int i = 0; i < data1.Length; i++)
            {
                if (data1[i] != data2[i])
                {
                    return false;
                }
            }
            return true;
        }

        //public static T ParseEnum<T>(string value) 
        //{
        //    return (T)Enum.Parse(typeof(T), value);
        //}

        #region Control Extensions

        /// <summary>
        /// Strongly-typed implementation of Control.FindControl method. Throws exception if control was not found.
        /// </summary>
        /// <typeparam name="T">Type of the control to be found.</typeparam>
        /// <param name="control">Target control.</param>
        /// <param name="id">The identifier of the control to be found.</param>
        /// <returns>Server control in the curernt naming container with the specified id parameter.</returns>
        public static T Find<T>(this Control control, string id) where T : class
        {
            return Find<T>(control, id, false);
        }

        public static T Find<T>(this Control control, string id, bool require) where T : class
        {
            Control c = control.FindControl(id);
            T t = c as T;
            if (require && c == null)
            {
                throw new InvalidOperationException(string.Format("Control {0} was not found.", id));
            }
            if (c != null && t == null)
            {
                throw new InvalidOperationException(string.Format("Expected {0} to be control of type {1}. Instead, it is of type {2}.",
                                                                  id, typeof(T).FullName, c.GetType().FullName));
            }
            return t;
        }

        public static void AppendAttribute(this IAttributeAccessor control, string key, string value)
        {
            string oldValue = control.GetAttribute(key);
            control.SetAttribute(key, string.IsNullOrEmpty(oldValue) ? value : oldValue + ' ' + value);
        }

        #endregion
    }
}